package com.studentexam.student.controller;

import com.studentexam.student.dao.QuestionBankMapper;
import com.studentexam.student.eo.QuestionBankEoPage;
import com.studentexam.student.model.QuestionBank;
import com.studentexam.student.service.QuestionBankService;
import com.studentexam.student.tool.ResponseMessage;
import com.studentexam.student.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags="questionbank")
@CrossOrigin
@RestController
public class QuestionBankController {
    @Autowired
    private QuestionBankService questionBankService;

    @ApiOperation(value = "题库信息添加方法")
    @RequestMapping(value = "/addqb", method = RequestMethod.GET)
    public ResponseMessage add(QuestionBank questionBank) {
        int isAdd = 0;
//        1.判断是否空对象
        if (questionBank != null) {
//               2.添加数据
            isAdd = questionBankService.addQuestionBank(questionBank);
            if (isAdd == 1) {
                return Result.success("保存成功");
            } else if (isAdd == -1) {
                return Result.success("已重复，不允许添加");
            } else return Result.error("保存失败");
        }
        return Result.error("保存失败");
    }


    @ApiOperation(value = "题库信息删除方法")
    @RequestMapping(value = "/delqb", method = RequestMethod.GET)
    public ResponseMessage delete(int questionbankid) {
        if (questionbankid != 0) {
            int isDel = 0;
            isDel = questionBankService.delQuestionBank(questionbankid);
            if (isDel > 0) {
                return Result.success("删除题库信息成功");
            } else return Result.error("删除题库信息失败");
        } else return Result.error("删除失败，题库编号不能为0");
    }

    @ApiOperation(value = "题库信息更新方法")
    @RequestMapping(value = "/updateqb", method = RequestMethod.GET)
    public ResponseMessage update(QuestionBank questionBank) {
        if (questionBank != null) {
            if (questionBank.getQuestionbankid() != 0) {
                if (questionBankService.updateQuestionBank(questionBank) > 0) {
                    return Result.success("更新题库信息成功");
                } else return Result.error("更新题库信息失败");
            } else return Result.error("更新题库信息失败，题库编号不能为0");
        } else return Result.error("更新题库信息失败，此对象不能为空");
    }

    @ApiOperation(value = "根据条件获取题库信息")
    @RequestMapping(value = "/getByQuestionBankid", method = RequestMethod.GET)
    public ResponseMessage getByRoleid(int questionbankid) {
        Map<String, List> map = new HashMap<>();
        List<QuestionBank> questionBankList = questionBankService.getByQuestionBankid(questionbankid);
        map.put("list", questionBankList);
        return Result.success("查询成功", map);
    }


    @ApiOperation(value = "根据题型和是否发布获取题库信息")
    @RequestMapping(value = "/getByType", method = RequestMethod.GET)
    public ResponseMessage getByType(String examtypeid, String isquestionrelease) {
        Map<String, List> map = new HashMap<>();
        List<QuestionBank> questionBankList = questionBankService.getByType(examtypeid, isquestionrelease);
        map.put("list", questionBankList);
        return Result.success("查询成功", map);
    }


    @ApiOperation(value = "根据条件获取题库信息")
    @RequestMapping(value = "/getQuestionBankWhereAll", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    private ResponseMessage getQuestionBankWhereAll(@RequestBody QuestionBankEoPage questionBankEoPage) {
        Map<String, List> map = new HashMap<>();
        List<QuestionBankEoPage> questionBankList = questionBankService.getQuestionBankWhereAll(questionBankEoPage);
        map.put("list", questionBankList);
        return Result.success("200", map);
    }
}
